//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ParametersTransformation.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (boost::shared_ptr<QuantLib::ParametersTransformation>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametersTransformation = NULL;
#endif
	_ppParametersTransformation = &childNative;
    
}
Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (QuantLib::ParametersTransformation& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phParametersTransformation = NULL;
#endif
	_ppParametersTransformation = new boost::shared_ptr<QuantLib::ParametersTransformation> (&childNative);
    
    _ParametersTransformationOwner = owner;
    
}

Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (CParametersTransformation^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametersTransformation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppParametersTransformation = new boost::shared_ptr<QuantLib::ParametersTransformation> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametersTransformation = NULL;
#endif
	if (!t->IsSubclassOf(CParametersTransformation::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (QuantLib::Handle<QuantLib::ParametersTransformation>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phParametersTransformation = &childNative;
	_ppParametersTransformation = &static_cast<boost::shared_ptr<QuantLib::ParametersTransformation>>(childNative.currentLink());
    
    _ParametersTransformationOwner = owner;
}
Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (QuantLib::Handle<QuantLib::ParametersTransformation> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phParametersTransformation = &childNative;
	_ppParametersTransformation = &static_cast<boost::shared_ptr<QuantLib::ParametersTransformation>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CParametersTransformation::CParametersTransformation (QuantLib::ParametersTransformation childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametersTransformation = NULL;
#endif
	_ppParametersTransformation = new boost::shared_ptr<QuantLib::ParametersTransformation> (new QuantLib::ParametersTransformation (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CParametersTransformation::~CParametersTransformation ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppParametersTransformation != NULL)
    {
	    delete _ppParametersTransformation;
        _ppParametersTransformation = NULL;
    }
}
Cephei::QL::Math::Optimization::CParametersTransformation::!CParametersTransformation ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppParametersTransformation != NULL)
    {
	    delete _ppParametersTransformation;
    }
}
QuantLib::ParametersTransformation& Cephei::QL::Math::Optimization::CParametersTransformation::GetReference ()
{
    if (_ppParametersTransformation == NULL) throw REFNEW NativeNullException ();
	return **_ppParametersTransformation;
}
boost::shared_ptr<QuantLib::ParametersTransformation>& Cephei::QL::Math::Optimization::CParametersTransformation::GetShared ()
{
    if (_ppParametersTransformation == NULL) throw REFNEW NativeNullException ();
	return *_ppParametersTransformation;
}
QuantLib::ParametersTransformation* Cephei::QL::Math::Optimization::CParametersTransformation::GetPointer ()
{
    if (_ppParametersTransformation == NULL) throw REFNEW NativeNullException ();
	return &**_ppParametersTransformation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ParametersTransformation>& Cephei::QL::Math::Optimization::CParametersTransformation::GetHandle ()
{
	if (_phParametersTransformation == NULL)
	{
		_phParametersTransformation = new Handle<QuantLib::ParametersTransformation> (*_ppParametersTransformation);
	}
	return *_phParametersTransformation;
}
#endif
bool Cephei::QL::Math::Optimization::CParametersTransformation::HasNative () 
{
	return (_ppParametersTransformation != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CParametersTransformation::Direct (Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Array _rv = (QuantLib::Array)(*_ppParametersTransformation)->direct ( _x );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CParametersTransformation::Inverse (Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Array _rv = (QuantLib::Array)(*_ppParametersTransformation)->inverse ( _x );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

